//
//  computer.cpp
//  Sinnca Engine v2
//
//  Created by Ryan Oldis on 6/13/11.
//  Copyright 2011 Sinnca Studios. All rights reserved.
//

#include "computer.h"

#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif



cpu* cpu::_instance = NULL;

cpu::cpu()
{
	vid = SDL_GetVideoInfo();
	SDL_VideoDriverName(driver, 128);
	
	/* Get available fullscreen/hardware modes */
	modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
	
	
	
	// ----------------------------------------------------------------------------
	// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
	// Thanks to http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c for the following code.
	#ifdef __APPLE__    
	CFBundleRef mainBundle = CFBundleGetMainBundle();
	CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
	char path[PATH_MAX];
	if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
	{
		// error!
	}
	CFRelease(resourcesURL);

	chdir(path);
	std::cout << "Current Path: " << path << std::endl;
	fullPath = path;
	#endif
	// ----------------------------------------------------------------------------
	
}

void cpu::printInfo()
{
	if (vid->hw_available == 1)
	{
		printf("Hardware acceleration available\n");
		printf("%d Megabytes of video memory\n", vid->video_mem/1024);
	} else {
		printf("WARNING! NO VIDEO CARD DETECTED!\n");
		printf("%d Megabytes of video memory\n", vid->video_mem/1024);
	}
	
	printf(driver);
	printf("\n");
	
	std::cout << "Current video resolution: " << vid->current_w << " x " << vid->current_h << "\n" << std::endl;
	
	/* Check if there are any modes available */
	if (modes == (SDL_Rect**)0) {
		printf("No modes available!\n");
		exit(-1);
	}
	
	/* Check if our resolution is restricted */
	if (modes == (SDL_Rect**)-1) {
		printf("All resolutions available.\n");
	}
	else{
		/* Print valid modes */
		printf("Available Modes\n");
		for (int i=0; modes[i]; ++i)
			printf("  %d x %d\n", modes[i]->w, modes[i]->h);
	}
}

cpu* cpu::Instance()
{
	if (_instance == NULL)
	{
		_instance = new cpu();
	}
	return _instance;
}